<template>
	<view class="root">
		<view class="title">
			<text>欢迎加入</text>
		</view>
		<view class="subTitle">
			<text>快来加入我们，共同关注护眼健康</text>
		</view>
		<view class="input">
			<up-icon name="account-fill" color="#41BC78" size="46rpx"></up-icon>
			<input type="text" @input="onPhoneInput" v-model="phone" placeholder="请输入手机号" />
		</view>
		<view class="tips" v-if="phoneTips !== ''">{{phoneTips}}</view>
		<view class="" style="height: 46rpx;"></view>
		<view class="input">
			<up-icon name="lock-fill" color="#41BC78" size="46rpx"></up-icon>
			<input type="password" v-model="passWord" @input="onPwdInput" placeholder="请输入密码" />
		</view>
		<view class="tips" v-if="pwdTips != ''">{{pwdTips}}</view>
		<view class="" style="height: 39rpx;"></view>
		<view class="link">
			<view class="text" @click="forgetPwd">
				<text>忘记密码</text>
			</view>
			<view class="text" @click="toregister">
				<text>新用户注册</text>
			</view>
		</view>
		<view class="submit" @click="submit">
			<view class="text">
				<text>立刻登陆</text>
			</view>
			<view class="icon">
				<view class="arrow"></view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from "vue";
	import {
		test
	} from '@/node_modules/uview-plus';
	import {
		login
	} from '@/api/index.js';
	import { toast } from '@/node_modules/uview-plus';

	const phone = ref('');
	const phoneTips = ref('');

	const passWord = ref('');
	const pwdTips = ref('');

	// 密码规则：字母/数字/特殊字符，长度6-20
	const pwdReg = /^[a-zA-Z0-9!@#$%^&*()_+]{8,20}$/;

	// 校验账号
	const onPhoneInput = (e) => {
		phoneTips.value = test.mobile(phone.value) ? '' : '手机号格式不正确';
	}
	// 校验密码
	function validatePwd() {
		if (!passWord.value) {
			pwdTips.value = "密码不能为空";
		} else if (!pwdReg.test(passWord.value)) {
			pwdTips.value = ""; // 密码需为8-20位字母、数字或特殊字符
		} else {
			pwdTips.value = "";
		}
	}

	// 输入时实时校验
	function onNameInput(e) {
		onPhoneInput();
	}

	function onPwdInput(e) {
		validatePwd();
	}

	// 提交
	function submit() {
		onPhoneInput();
		validatePwd();
		if (!phoneTips.value && !pwdTips.value) {
			login({
				phone: phone.value,
				userPW: passWord.value,
				loginType: 'user_pw'
			}).then(res => {
				if(res.code === 200) {
					toast('登录成功');
					const token = res.data.userToken
					if(token) {
						uni.setStorageSync('token',token)
					}
					setTimeout(() => {
						// 判断是否为初次进入
						// if (uni.getStorageSync('isFirst')) {
						// 	uni.redirectTo({
						// 		url: '/pages/main/index'
						// 	})
						// } else {
						// 	uni.redirectTo({
						// 		url: '/pages/index/index'
						// 	})
						// }
						uni.reLaunch({
							url:'/pages/launch/launch'
						})
					}, 1500)
				}
			})
		}else {
			toast('账号或密码不可为空');
		}
	}
	const init = () => {
		phone.value = ''
		phoneTips.value = ''
		passWord.value = ''
		pwdTips.value = ''
	}

	function forgetPwd() {
		init()
		uni.navigateTo({
			url: `/pages/forgetPwd/index`
		});
	}

	function toregister() {
		init()
		uni.navigateTo({
			url: `/pages/register/index`
		});
	}
</script>


<style lang="less" scoped>
	.root {
		min-height: 100vh;
		background-color: #FFFFFF;
		padding-bottom: 10rpx;
		box-sizing: border-box;

		.title {
			padding-top: 239rpx;
			font-family: PingFangSC, PingFang SC;
			font-weight: 600;
			font-size: 63rpx;
			color: #000000;
			letter-spacing: 4rpx;
			text-align: center;
		}

		.subTitle {
			font-family: PingFangSC, PingFang SC;
			font-size: 32rpx;
			color: rgba(0, 0, 0, 0.49);
			text-align: center;
			margin-top: 14rpx;
			font-weight: 600;
			margin-bottom: 186rpx;
		}

		.input {
			width: 666rpx;
			margin: 0 auto 0 auto;
			position: relative;

			:deep(.u-icon__icon) {
				position: absolute;
				right: 44rpx;
				top: 0;
				bottom: 0;
			}

			input {
				width: 100%;
				height: 123rpx;
				background-color: #F5F5F5;
				border-radius: 35rpx;
				padding-left: 35rpx;
				padding-right: 100rpx;
				box-sizing: border-box;
				font-weight: 300;
				font-size: 32rpx;
				color: #000000;
				border: 4rpx solid #F5F5F5;
				transition: .3s;
			}

			input:hover,
			input:focus {
				border: 4rpx solid #E72222;
			}
		}

		.link {
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;
			width: 666rpx;
			margin: 0 auto;
			padding: 0 23rpx;
			box-sizing: border-box;

			.text {
				font-family: PingFangSC, PingFang SC;
				font-weight: 600;
				font-size: 25rpx;
				color: #000000;
				border-bottom: 2rpx solid #000;
			}
		}

		.submit {
			width: 666rpx;
			height: 123rpx;
			margin: 366rpx auto 20rpx auto;
			box-sizing: border-box;
			padding: 40rpx 47rpx 39rpx 33rpx;
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;
			background-color: #035A36;
			border-radius: 35rpx;

			.text {
				font-family: PingFangSC, PingFang SC;
				font-weight: 600;
				font-size: 32rpx;
				color: #FFFFFF;
			}

			.icon {
				width: 72rpx;
				height: 2rpx;
				background-color: #fff;
				position: relative;

				.arrow {
					width: 16rpx;
					height: 16rpx;
					background: linear-gradient(45deg, transparent 0%, transparent 50%, #fff 50%, #fff 100%);
					position: absolute;
					top: 0;
					bottom: 0;
					right: 0;
					margin: auto 0;
					transform: rotateZ(45deg);
				}
			}
		}
	}

	.tips {
		padding-left: 44rpx;
		font-size: 24rpx;
		color: #E72222;
		margin: 8rpx 0;
	}
</style>